package com.yusys.activiti;

import org.activiti.engine.*;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Before;
import org.junit.Test;

import java.io.InputStream;
import java.util.List;
import java.util.zip.ZipInputStream;

public class Init {

    private static ProcessEngine engine;

    @Before
    public void before() {
        engine = ProcessEngines.getDefaultProcessEngine();
    }

    /**
     * 初始化工作流程
     */
    @Test
    public void test() {
        ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
        System.out.println(engine);
    }

    /**
     * 自定义配置文件创建工作流
     */
    @Test
    public void test2() {
        ProcessEngineConfiguration configuration = ProcessEngineConfiguration
                .createProcessEngineConfigurationFromResource("activiti.cfg.xml");
        ProcessEngine processEngine = configuration.buildProcessEngine();
        System.out.println(processEngine);
    }

    /**
     * 部署单个文件
     */
    @Test
    public void test3() {
        // 1.加载工作流引擎
        ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
        // 2.获取仓库service
        RepositoryService repositoryService = engine.getRepositoryService();
        // 3.装载外部文件
        Deployment deploy = repositoryService.createDeployment()
                .addClasspathResource("bpmn/evection.bpmn")
                .addClasspathResource("bpmn/evection.png")
                .name("出差申请单")
                .deploy();
        System.out.println("部署出差流程ID：" + deploy.getId());
        System.out.println("部署出差流程名称：" + deploy.getName());
    }

    @Test
    public void test4() {
        // 1.加载工作流引擎
        ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
        // 2.获取仓库service
        RepositoryService repositoryService = engine.getRepositoryService();
        // 3.获取外部文件
        InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("bpmn/evection.zip");
        if (inputStream != null) {
            ZipInputStream zipInputStream = new ZipInputStream(inputStream);
            Deployment deploy = repositoryService.createDeployment()
                    .addZipInputStream(zipInputStream)
                    .name("出差申请单")
                    .deploy();
            System.out.println("部署出差流程ID：" + deploy.getId());
            System.out.println("部署出差流程名称：" + deploy.getName());
        }
    }

    /**
     * 启动一个流程实例
     */
    @Test
    public void test5() {
        ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
        RuntimeService service = engine.getRuntimeService();
        String id = "evection";
        ProcessInstance instance = service.startProcessInstanceByKey(id);
        System.out.println("流程定义ID：" + instance.getProcessDefinitionId());
        System.out.println("流程实例ID：" + instance.getId());
        System.out.println("当前活动ID：" + instance.getActivityId());
    }

    /**
     * 任务查询
     */
    @Test
    public void test6() {
        String assignee = "zhansan";
        ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
        // 获取任务service
        TaskService taskService = engine.getTaskService();
        List<Task> tasks = taskService.createTaskQuery()
                .processDefinitionKey("evection")
                .taskAssignee(assignee)
                .list();
        for (Task task : tasks) {
            System.out.println(task);
        }
    }

    /**
     * 流程处理
     */
    @Test
    public void test7() {
        // 获取任务service
        TaskService taskService = engine.getTaskService();

        Task task = taskService.createTaskQuery()
                .processDefinitionKey("evection")
                .taskAssignee("zhansan")
                .singleResult();
        taskService.complete(task.getId());
    }

    /**
     * 查询流程的定义
     */
    @Test
    public void test8() {
        RepositoryService repositoryService = engine.getRepositoryService();
        List<ProcessDefinition> list = repositoryService
                .createProcessDefinitionQuery()
                .processDefinitionKey("evection")
                .orderByProcessDefinitionVersion()
                .desc().list();
        for (ProcessDefinition processDefinition : list) {
            System.out.println(processDefinition);
        }
    }

    @Test
    public void test9() {
        RepositoryService repositoryService = engine.getRepositoryService();
        repositoryService.deleteDeployment("3333");
        // 及联删除
        // repositoryService.deleteDeployment("444",true);
    }
}
